package ezxpns.cmd;

import java.awt.event.*;
import javax.swing.*;
import ezxpns.Application.*;
import ezxpns.gui.tab.*;

@SuppressWarnings("serial")
public class CmdEdit
{
	private AppKey app;
	public EditUndo editUndo;
	public EditRedo editRedo;
	public EditHop editHop;
	public CmdEdit(AppKey app2)
	{
		app=app2;
		editUndo=new EditUndo();
		editRedo=new EditRedo();
		editHop=new EditHop();
	}
	public class EditUndo extends AbstractAction
	{
		EditUndo()
		{
			String name="Undo";
			String desc="Undo edit";
			String key="control Z";
			putValue(Action.NAME,name);
			putValue(Action.SHORT_DESCRIPTION,desc);
			putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(key));
			app.root.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),key);
			app.root.getRootPane().getActionMap().put(key,this);
		}
		public void exec()
		{
			((TabGUIDefault)app.tabs.getSelectedComponent()).undo();
			app.root.validate();
		}
		public void update()
		{
			if( app.tabs.getTabCount()==0 ) { setEnabled(false); return; }
			setEnabled(((TabGUIDefault)app.tabs.getSelectedComponent()).canUndo());
		}
		public void actionPerformed(ActionEvent event)
		{
			exec();
		}
	}
	public class EditRedo extends AbstractAction
	{
		EditRedo()
		{
			String name="Redo";
			String desc="Redo edit on last branch";
			String key="control Y";
			putValue(Action.NAME,name);
			putValue(Action.SHORT_DESCRIPTION,desc);
			putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(key));
			app.root.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),key);
			app.root.getRootPane().getActionMap().put(key,this);
		}
		public void exec()
		{
			((TabGUIDefault)app.tabs.getSelectedComponent()).redo();
		}
		public void update()
		{
			if( app.tabs.getTabCount()==0 ) { setEnabled(false); return; }
			setEnabled(((TabGUIDefault)app.tabs.getSelectedComponent()).canRedo());
		}
		public void actionPerformed(ActionEvent event)
		{
			exec();
		}
	}
	public class EditHop extends AbstractAction
	{
		EditHop()
		{
			String name="Hop";
			String desc="Hop to previous branch";
			String key="control B";
			putValue(Action.NAME,name);
			putValue(Action.SHORT_DESCRIPTION,desc);
			putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(key));
			app.root.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),key);
			app.root.getRootPane().getActionMap().put(key,this);
		}
		public void exec()
		{
			((TabGUIDefault)app.tabs.getSelectedComponent()).hop();
		}
		public void update()
		{
			if( app.tabs.getTabCount()==0 ) { setEnabled(false); return; }
			setEnabled(((TabGUIDefault)app.tabs.getSelectedComponent()).canHop());
		}
		public void actionPerformed(ActionEvent event)
		{
			exec();
		}
	}
	public void update()
	{
		editUndo.update();
		editRedo.update();
		editHop.update();
	}
}
